package me.zhengjie.minbearwcs.dao.storage.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import lombok.RequiredArgsConstructor;
import me.zhengjie.minbearwcs.dao.storage.inf.LargeStorageDao;
import me.zhengjie.minbearwcs.entity.Mapper.storage.LargeStorageMapper;
import me.zhengjie.minbearwcs.entity.PO.wcs.storage.LargeStorage;
import me.zhengjie.minbearwcs.entity.PO.wcs.storage.type.StorageType;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Repository
@RequiredArgsConstructor
public class LargeStorageDaoImpl implements LargeStorageDao {

    private final LargeStorageMapper mapper;

    @Override
    public List<String> selectLargeStorageCodeByFloorNum(String floorNum) {
        return new LambdaQueryChainWrapper<>(mapper)
                .eq(LargeStorage::getFloorNum, floorNum)
                .select(LargeStorage::getLargeStorageCode)
                .list()
                .stream()
                .map(LargeStorage::getLargeStorageCode)
                .collect(Collectors.toList());
    }

    @Override
    public LargeStorage selectByLargeStorageCode(String largeStorageCode) {
        return new LambdaQueryChainWrapper<>(mapper)
                .eq(LargeStorage::getLargeStorageCode, largeStorageCode)
                .oneOpt()
                .orElse(null);
    }

    @Override
    public String selectLargeStorageCodeByLogisticsCompany(String logisticsCompany) {
        Optional<LargeStorage> largeStorage = new LambdaQueryChainWrapper<>(mapper)
                .eq(LargeStorage::getLogisticsCompanyCode, logisticsCompany)
                .select(LargeStorage::getLargeStorageCode)
                .oneOpt();
        return largeStorage.isPresent() ? largeStorage.get().getLargeStorageCode() : null;
    }

    @Override
    public String selectNullLogisticsCompanyByStorageType(StorageType storageType) {
        Optional<LargeStorage> largeStorage = new LambdaQueryChainWrapper<>(mapper)
                .eq(LargeStorage::getLargeStorageType, storageType)
                .isNull(LargeStorage::getLogisticsCompanyCode)
                .select(LargeStorage::getLargeStorageCode)
                .last("limit 1")
                .oneOpt();
        return largeStorage.isPresent() ? largeStorage.get().getLargeStorageCode() : null;
    }

    @Override
    public boolean updateLogisticsCompanyByLargeStorage(String largeStorageCode, String logisticsCompany) {
        return new LambdaUpdateChainWrapper<>(mapper)
                .eq(LargeStorage::getLargeStorageCode, largeStorageCode)
                .set(LargeStorage::getLogisticsCompanyCode, logisticsCompany)
                .update();
    }

    @Override
    public boolean insert(LargeStorage largeStorage) {
        return mapper.insert(largeStorage) == 1;
    }

    @Override
    public boolean clear() {
        return mapper.delete(new QueryWrapper<>()) == 1;
    }

}
